package com.springcloud.util;

import static com.netflix.zuul.context.RequestContext.getCurrentContext;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.netflix.zuul.context.RequestContext;

public class GetPostData {

	public static String getToken(HttpServletRequest request, RequestContext ctx) {

		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart(request);
		String token = multipartRequest.getParameter("token");
		ctx.setRequest(httpServletRequestWrapper(multipartRequest));
		// String token = null;
		// BufferedReader br = null;
		// InputStream is = null;
		// try {
		// StringBuffer sb = new StringBuffer();
		// is = request.getInputStream();
		// InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
		// br = new BufferedReader(isr);
		// String readLine = null;
		// while ((readLine = br.readLine()) != null) {
		// sb.append(readLine);
		// System.err.println(readLine);
		// }
		// br = request.getReader();
		// StringBuffer sb = new StringBuffer();
		// String readLine;
		// while ((readLine = br.readLine()) != null) {
		// sb.append(readLine);
		// }
		// JSONObject re = JSONObject.parseObject(sb.toString());
		// token = re.get("token").toString();
		// System.err.println("token:" + re.get("token"));
		// } catch (IOException e) {
		// e.printStackTrace();
		// } finally {
		// try {
		// br.close();
		// // is.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }

		return token;

	}

	public static String getToken(HttpServletRequest request) {
		HttpServletRequestWrapper httpServletRequestWrapper = new HttpServletRequestWrapper(request) {
			@Override
			public ServletInputStream getInputStream() throws IOException {
				// return new ServletInputStreamWrapper(request.getInputStream());
				return request.getInputStream();
			}

			@Override
			public int getContentLength() {
				// return bytes.length;
				return request.getContentLength();
			}

			@Override
			public long getContentLengthLong() {
				// return bytes.length;
				return request.getContentLengthLong();
			}
		};
		// 拿到参数，再把request放回上下文
		// getCurrentContext().setRequest(httpServletRequestWrapper);
		String token = httpServletRequestWrapper.getParameter("token");
		return token;

	}

	public static final byte[] input2byte(InputStream inStream) throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[1024];
		int rc = 0;
		while ((rc = inStream.read(buff, 0, 1024)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	}

	public static HttpServletRequestWrapper httpServletRequestWrapper(MultipartHttpServletRequest request) {
		HttpServletRequestWrapper httpServletRequestWrapper = new HttpServletRequestWrapper(
				getCurrentContext().getRequest()) {
			@Override
			public ServletInputStream getInputStream() throws IOException {
				// return new ServletInputStreamWrapper(request.getInputStream());
				return request.getInputStream();
			}

			@Override
			public int getContentLength() {
				// return bytes.length;
				return request.getContentLength();
			}

			@Override
			public long getContentLengthLong() {
				// return bytes.length;
				return request.getContentLengthLong();
			}
		};
		return httpServletRequestWrapper;

	}
}
